package com.qd.panda.shop.product;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.panda.domain.entity.shop.product.ProductDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.panda.service.shop.product.ProductImgService;
import com.qd.panda.service.shop.product.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * WEB_商品信息
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/product/")
@Slf4j
public class ProductController {
    private final ProductService productService;
    private final ProductImgService productImgService;

    /**
     * 添加商品信息
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:product:save')")
    @OperatorInfo(title = "添加", operator = OperatorType.INSERT)
    @PostMapping(value = "/save")
    public ProductDTO save(@Validated({DefaultGroup.class}) @RequestBody ProductDTO dto) {
        dto.setProductCode(IdWorker.getIdStr());
        return productService.save(dto);
    }

    /**
     * 修改商品信息
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:product:edit')")
    @OperatorInfo(title = "修改", operator = OperatorType.UPDATE)
    @PutMapping(value = "/update")
    public ProductDTO update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody ProductDTO dto) {
        dto.setUpdateTime(LocalDateTime.now());
        return productService.update(dto);
    }

    /**
     * 删除商品信息
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:product:delete')")
    @OperatorInfo(title = "删除", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            productService.removeById(ids.get(0));
        } else {
            productService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取商品信息
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ProductDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return productService.getByIdDto(id);
    }

    /**
     * 商品信息列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<ProductDTO> page(ProductDTO dto) {
        return productService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 删除商品图片
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:productImg:delete')")
    @OperatorInfo(title = "删除商品图片", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/deleteImg")
    public ApiResultWrapper<Object> deleteImg(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            productImgService.removeById(ids.get(0));
        } else {
            productImgService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 更新商品状态
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:product:updateState')")
    @OperatorInfo(title = "修改", operator = OperatorType.UPDATE)
    @PutMapping(value = "/updateState")
    public ApiResultWrapper<String> updateState(@Validated @RequestBody ProductDTO dto) {
        if (!StringUtils.hasLength(dto.getProductCode())) {
            return ApiResult.error("主键不能为空");
        }
        if (null == dto.getState()) {
            return ApiResult.error("商品状态不能为空");
        }
        productService.updateState(dto);
        return ApiResult.success();
    }
}